package com.guaoran.source.mybatis.v2.session;

import com.guaoran.source.mybatis.v2.config.Configuration;
import com.guaoran.source.mybatis.v2.execute.Executor;
import com.guaoran.source.mybatis.v2.mapper.MapperProxy;
import com.guaoran.source.mybatis.v2.mapper.MapperRegistry;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
import java.sql.SQLException;
import java.util.List;

/**
 * @author : 孤傲然
 * @Description :
 * @date :2018/5/26 11:29
 */
public class SqlSession {
    private Configuration configuration;
    private Executor executor;

    public SqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    public <T> T getMapper(Class<T> clazz){
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new MapperProxy(this));
    }

    public <T> T selectOne(MapperRegistry.KnownMappersInfo knownMappersInfo, Object arg) throws SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return (T) executor.query(knownMappersInfo, arg);
    }
    public <T> List<T> selectList(MapperRegistry.KnownMappersInfo knownMappersInfo, Object arg) throws SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return executor.queryList(knownMappersInfo, arg);
    }
}
